<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-waves v-permission="['metadata:MetadataLabelGroup:add']" plain class="filter-item"
        style="margin-left: 10px" type="primary" icon="el-icon-edit" @click="handleCreate">
        {{ $t('table.add') }}
      </el-button>
    </div>

    <el-table ref="table" :key="tableKey" v-loading="listLoading" element-loading-text="正在加载，请稍后" :data="list" border
      fit highlight-current-row style="width: 100%">
      <el-table-column label="标签值" align="center" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.label }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="标签规则" align="center" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.whereType | dictLabel('sql_where_type')
            }}{{ row.whereType | (dictLabel('sql_where_type') == '是否为空') ? '' : row.whereType |
              (dictLabel('sql_where_type') == '是否不为空') ? '' : row.value }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="标签规则" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.whereType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标签值" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.value }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" min-width="230" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button v-permission="['metadata:MetadataLabelGroup:update']" type="text" size="small" icon="el-icon-edit"
            @click="handleUpdate(row)">
            {{ $t('table.edit') }}
          </el-button>
          <el-button v-permission="['metadata:MetadataLabelGroup:delete']" type="text" size="small"
            icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(row)">
            {{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      :page-sizes="pageArray" @pagination="getList" />

    <metadata-label-form ref="form" @refreshList="getList" />
  </div>
</template>

<script>
import { deleteMetadataLabel, fetchMetadataLabelList } from '@/api/metadata/label/metadataLabel'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import metadataLabelForm from './metadataLabelForm'
import Pagination from '@/components/Pagination'

export default {
  name: 'MetadataLabelView',
  components: { metadataLabelForm, Pagination },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: false,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      },
    }
  },
  methods: {
    refreshSub(row) {
      this.listQuery.gid = row.id
      this.listQuery.page = 1
      this.listQuery.keyword = ''
      this.getList()
    },
    getList() {
      this.listLoading = true
      fetchMetadataLabelList(this.listQuery).then((response) => {
        console.log(response.data.data, '-->');
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleCreate() {
      if (this.isNull(this.listQuery.gid)) {
        this.$message.warning('请先选择一条上级数据')
        return
      }
      this.$refs.form.handleCreate(this.listQuery.gid)
    },
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row.id)
    },
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteMetadataLabel(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}
</style>
